package com.example.yrdev.ordering_system.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Adapter.ShoppingCartAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Model.Good;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 11/18/16.
 */

public class ShoppingCartActivity extends CommonActivity {
    @InjectView(R.id.total_price)TextView total_price;
    @InjectView(R.id.item_List)ListView listView;
    private ArrayList<Integer> purchaseCountList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_cart_layout);
        ButterKnife.inject(this);
        setBarTitle("购物车");
        initView();

    }
   ArrayList<Good> shoppingList;
    Integer currentPurchaseCount;
Good  respGood;
    ShoppingCartAdapter shoppingCartAdapter;


    @Override
    public void initView() {

        super.initView();
        shoppingList=(ArrayList<Good>) getIntent().getSerializableExtra("shopList");
        purchaseCountList=new ArrayList<>();

        shoppingCartAdapter=new ShoppingCartAdapter(this,shoppingList);
        listView.setAdapter(shoppingCartAdapter);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


             respGood=shoppingList.get(i);
              checkCorrespondView(view);


           }
       });
        shoppingCartAdapter.setShoppingCartListener(new ShoppingCartAdapter.ShoppingCartListener() {

            @Override
            public void reloadPrice() {

                checkCurrentPrice();

            }


            @Override
            public void goBack() {
                
                setResult(RESULT_OK);
                finish();

            }

            @Override
            public void calcVal(String viewPosition, int butId) {
                     currentPurchaseCount=purchaseCountList.get(Integer.valueOf(viewPosition));
                     respGood=shoppingList.get(Integer.valueOf(viewPosition));



                switch (butId){
                    case R.id.min:
                        if (currentPurchaseCount>1){

                            currentPurchaseCount=currentPurchaseCount-1;

                        }


                        break;

                    case R.id.add:
                        if (currentPurchaseCount< Integer.valueOf(respGood.getStoredNumber()))
                        currentPurchaseCount=currentPurchaseCount+1;

                        break;

                }
                purchaseCountList.set(Integer.valueOf(viewPosition),currentPurchaseCount);


                reloadStoredNumber(viewPosition);
            }


            @Override
            public void delItem(String viewPosition) {

                shoppingCartAdapter.removeItem(Integer.valueOf(viewPosition));

                checkCurrentPrice();

                if (shoppingCartAdapter.getShoppingList().size()==0){

                   setResult(RESULT_OK);

                    finish();

               }


            }
        });



        checkCurrentPrice();
        getCurrentPurchaseCountList();
    }

    View respView;
    View respDetailView;

    private void checkCorrespondView(View view){

        if (respView!=view){



            if (respView!=null){
                View shortDetail=respView.findViewById(R.id.shortDetail);
                View detailView=respView.findViewById(R.id.detailInfo);
              View arrow=respView.findViewById(R.id.arrow);
                TextView purchaseCount=(TextView)respView.findViewById(R.id.tempStoredNumber);
                TextView totalPrice=(TextView)respView.findViewById(R.id.tempTotalPrice);

                 detailView.setVisibility(View.GONE);
                arrow.setSelected(false);
                shortDetail.setVisibility(View.VISIBLE);
                purchaseCount.setText("x"+respGood.getPurchaseCount());
                totalPrice.setText(respGood.getTotalPrice()+"元");

                respView=view;
                View temp=respView.findViewById(R.id.detailInfo);
                shortDetail=respView.findViewById(R.id.shortDetail);
                arrow=respView.findViewById(R.id.arrow);
                arrow.setSelected(true);
                temp.setVisibility(View.VISIBLE);
                shortDetail.setVisibility(View.GONE);

                respView=view;

            }else {

                respView=view;
                View temp=respView.findViewById(R.id.detailInfo);
                View shortDetail=respView.findViewById(R.id.shortDetail);
                View arrow=respView.findViewById(R.id.arrow);
                arrow.setSelected(true);
                temp.setVisibility(View.VISIBLE);
                shortDetail.setVisibility(View.GONE);
            }
        }else {

            respView=view;
            View temp=respView.findViewById(R.id.detailInfo);
            View shortDetail=respView.findViewById(R.id.shortDetail);
            TextView purchaseCount=(TextView)respView.findViewById(R.id.tempStoredNumber);
            TextView totalPrice=(TextView)respView.findViewById(R.id.tempTotalPrice);
            View arrow=respView.findViewById(R.id.arrow);




            if (temp.getVisibility()==View.VISIBLE){
                temp.setVisibility(View.GONE);
                shortDetail.setVisibility(View.VISIBLE);
                purchaseCount.setText("x"+respGood.getPurchaseCount());
                totalPrice.setText(respGood.getTotalPrice()+"元");
                arrow.setSelected(false);
            }

            else{
                shortDetail.setVisibility(View.GONE);
                temp.setVisibility(View.VISIBLE);
                arrow.setSelected(true);
            }

        }
    }
    private void getCurrentPurchaseCountList(){
        for (Good good:shoppingList)
            purchaseCountList.add(good.getPurchaseCount());
    }

    private void reloadStoredNumber(String viewPosition){

        int fetch;
        if (listView.getLastVisiblePosition() >=listView.getChildCount()) {
            fetch = listView.getChildCount() - 1 - (listView.getLastVisiblePosition() - Integer.valueOf(viewPosition) );
        } else {
            fetch = Integer.valueOf(viewPosition);

        }

        View respView = listView.getChildAt(fetch);


        String val= String.valueOf(currentPurchaseCount);


        respGood.setPurchaseCount(Integer.valueOf(val));


        TextView storedNumber=(TextView)respView.findViewById(R.id.purchaseCount);
        storedNumber.setText(val);



        TextView totalPrice=(TextView)respView.findViewById(R.id.total_price);
        totalPrice.setText(respGood.getTotalPrice()+"元");


        checkCurrentPrice();


    }

    private   void checkCurrentPrice(){
        double price=0;

        for (int i=0;i<shoppingList.size();i++){
            Good goods=shoppingList.get(i);
            price=price+ Double.valueOf(goods.getTotalPrice());


        }

        total_price.setText( String.format(Locale.getDefault(),"%.2f",price)+"元");

    }


    public void conShop(View view){
        Intent intent=new Intent();
        intent.putExtra("shopList",shoppingList);
       setResult(RESULT_FIRST_USER,intent);
        finish();
    }

    public void goPay(View view){

        Intent intent=new Intent();
        intent.putExtra("shopbag",shoppingList);

        gotoActivityWithIntent(intent,PaymentActivity.class,0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK){

            setResult(RESULT_OK);
            finish();
        }



    }
}
